package cn.iocoder.an.module.business.controller.admin.equipmentcheckrecord;

import cn.iocoder.an.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.an.module.business.controller.admin.equipmentcheckrecorddetail.vo.EquipmentCheckRecordDetailRespVO;
import cn.iocoder.an.module.business.dal.dataobject.equipmentcheckrecorddetail.EquipmentCheckRecordDetailDO;
import cn.iocoder.an.module.business.dal.mysql.equipmentcheckrecorddetail.EquipmentCheckRecordDetailMapper;
import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.equipmentcheckrecord.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.equipmentcheckrecord.EquipmentCheckRecordDO;
import cn.iocoder.an.module.business.service.equipmentcheckrecord.EquipmentCheckRecordService;

@Tag(name = "管理后台 - 点检记录")
@RestController
@RequestMapping("/business/equipment-check-record")
@Validated
public class EquipmentCheckRecordController {

    @Resource
    private EquipmentCheckRecordService equipmentCheckRecordService;

    @Resource
    private EquipmentCheckRecordDetailMapper equipmentCheckRecordDetailMapper;

    @PostMapping("/create")
    @Operation(summary = "创建点检记录")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-record:create')")
    public CommonResult<Long> createEquipmentCheckRecord(@Valid @RequestBody EquipmentCheckRecordSaveReqVO createReqVO) {
        return success(equipmentCheckRecordService.createEquipmentCheckRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新点检记录")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-record:update')")
    public CommonResult<Boolean> updateEquipmentCheckRecord(@Valid @RequestBody EquipmentCheckRecordSaveReqVO updateReqVO) {
        equipmentCheckRecordService.updateEquipmentCheckRecord(updateReqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除点检记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:equipment-check-record:delete')")
    public CommonResult<Boolean> deleteEquipmentCheckRecord(@RequestParam("id") Long id) {
        equipmentCheckRecordService.deleteEquipmentCheckRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除点检记录")
                @PreAuthorize("@ss.hasPermission('business:equipment-check-record:delete')")
    public CommonResult<Boolean> deleteEquipmentCheckRecordList(@RequestParam("ids") List<Long> ids) {
        equipmentCheckRecordService.deleteEquipmentCheckRecordListByIds(ids);
        return success(true);
    }

    @TransMethodResult
    @GetMapping("/get")
    @Operation(summary = "获得点检记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-record:query')")
    public CommonResult<EquipmentCheckRecordRespVO> getEquipmentCheckRecord(@RequestParam("id") Long id) {
        EquipmentCheckRecordDO equipmentCheckRecord = equipmentCheckRecordService.getEquipmentCheckRecord(id);
        EquipmentCheckRecordRespVO bean = BeanUtils.toBean(equipmentCheckRecord, EquipmentCheckRecordRespVO.class);
        LambdaQueryWrapperX<EquipmentCheckRecordDetailDO> detailWrapperX = new LambdaQueryWrapperX<>();
        detailWrapperX.eq(EquipmentCheckRecordDetailDO::getRecordId,bean.getId());
        List<EquipmentCheckRecordDetailDO> equipmentCheckRecordDetailDOS = equipmentCheckRecordDetailMapper.selectList(detailWrapperX);
        List<EquipmentCheckRecordDetailRespVO> details = BeanUtils.toBean(equipmentCheckRecordDetailDOS, EquipmentCheckRecordDetailRespVO.class);
        bean.setDetailDO(details);
        return success(bean);
    }

    @TransMethodResult
    @GetMapping("/page")
    @Operation(summary = "获得点检记录分页")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-record:query')")
    public CommonResult<PageResult<EquipmentCheckRecordRespVO>> getEquipmentCheckRecordPage(@Valid EquipmentCheckRecordPageReqVO pageReqVO) {
        PageResult<EquipmentCheckRecordDO> pageResult = equipmentCheckRecordService.getEquipmentCheckRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentCheckRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出点检记录 Excel")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEquipmentCheckRecordExcel(@Valid EquipmentCheckRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentCheckRecordDO> list = equipmentCheckRecordService.getEquipmentCheckRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "点检记录.xls", "数据", EquipmentCheckRecordRespVO.class,
                        BeanUtils.toBean(list, EquipmentCheckRecordRespVO.class));
    }

}